#ifndef TIME_MGMT_H
#define TIME_MGMT_H

#include <vector>
#include "common.h"

void test();


class timers {
public:
    static timers &instance() { static timers tinstance; return tinstance; }

    static FunctionSuccesFailure timeout_create(UINT ms_interval);
    static INT timer_create(UINT ms_interval, VOID (*ext_funct)(VOID));
    static INT timeout_peek_create(UINT ms_delay);
    static INT timeout_peek_left_ms(UINT timeout);
    static FunctionSuccesFailure timer_kill(UINT timer);
    static FunctionSuccesFailure timeout_kill(UINT timeout); 

private:
    static const enum timerKind  { IDT_KUNDEF=0, IDT_KTIMER, IDT_KTIMEOUT   };

    timers() {}
    timers(const timers&);
    timers &operator=(const timers&);

    struct tim_info{
        tim_info():taken(FALSE), hKillEventRaw(0), Type(IDT_KUNDEF), TimeoutDelayMs(0), hThreadRaw(0), fct(NULL), ms_interval(0) {}
        tim_info(timerKind tKind, UINT_PTR hEvtRaw, UINT Timeout, UINT_PTR hThdRaw, VOID (*f)(VOID), UINT ms_ival):
                taken(TRUE), hKillEventRaw(hEvtRaw), Type(tKind), TimeoutDelayMs(Timeout), hThreadRaw(hThdRaw), fct(f), ms_interval(ms_ival) {}

        BOOL      taken;
        timerKind Type;
        UINT_PTR  hKillEventRaw;
        UINT      TimeoutDelayMs;
        UINT_PTR  hThreadRaw;
        VOID      (*fct)(VOID);
        UINT      ms_interval;
    };

    static std::vector<tim_info> timersData;
    static const UINT_PTR timerMutex;
    static const UINT MaxTimersLimit = 20;

    static BOOL isTimeoutDeployed();
    static BOOL isTimeoutLast();
    static FunctionSuccesFailure getFreeTimer(UINT &);
    static FunctionSuccesFailure destroyTimerThread(UINT);

    static VOID updatePeekTimeouts();
    static FunctionSuccesFailure setTaken(UINT, timerKind, UINT_PTR, UINT, UINT_PTR, VOID (*)(VOID), UINT);
    static FunctionSuccesFailure releaseTaken(UINT released);

    friend VOID timoutUpdateFunction();
};


#endif
